// pages/qualityControl/executeControl/addQuality/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nowdata:{},
    code:"",
    allList:[],
    addList:[],
    qcItemCategory:"",
    content:"",
    qcItem:"",
    syy:true,
    xyy:false,
    result:"",
    iosDialog1: false,
    thresholdRange:"",
    num:0,
    qcPeriod:"",
    date:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    let self = this
     const eventChannel = this.getOpenerEventChannel();
		// 监听 index页面定义的toHome 事件
		eventChannel.on('toaddqua', (res) => {
      this.setData({
        nowdata:res.item,
        code:res.code,
        allList:res.allData,
        date:res.date
      })
      console.log(res,'页面传值') 
      self.data.nowdata.forEach(e=>{
        self.data.allList.forEach(a=>{
          a.result=""
          if(e==a.qcItemCategory){
            self.data.addList.push(a)
          }
        })
  
        console.log(e)
      })
      console.log(self.data.addList)
      self.setData({
        qcItemCategory:self.data.addList[0].qcItemCategory,
        qcItem:self.data.addList[0].qcItem,
        addList:self.data.addList,
        result:self.data.addList[0].result,
        thresholdRange:self.data.addList[0].thresholdRange,
        qcPeriod:self.data.addList[0].qcPeriod
      })
    })


  },
      // 展示信息
      showModal(error){
        wx.showModal({
          content: error.msg,
          showCancel: false,
        })
      },
  close: function () {
    this.setData({
        iosDialog1: false,
    })
},
  numadd(){
    let self = this
    if(self.data.num==self.data.addList.length-2){
      self.setData({
        xyy:true
      })
    }else{
      self.setData({
        syy:false
      })
    }
    console.log(self.data.result)
    self.data.addList[self.data.num].result = self.data.result
    let patt1=new RegExp("^(-{0,1}[0-9])+(.[0-9]{1,})?$");
    let juge2=""
    let patt2 = RegExp("^[YN]$");
    if(self.data.thresholdRange!=""&&self.data.thresholdRange!=undefined){
      console.log(self.data.thresholdRange)
      juge2 = patt2.test(self.data.thresholdRange)
      console.log(juge2)
      if(juge2){
        let juge =  patt2.test(self.data.result)
        console.log(juge)
      if(!juge){
      self.showModal({msg:'请填写正确检查结果'})
      return
    }
      }else{
        console.log(juge2)
        let juge =  patt1.test(self.data.result)
        if(!juge){
          self.showModal({msg:'请填写正确检查结果'})
          return
        }
      }
    }else{
      let patt3=new RegExp("^(-{0,1}[0-9])+(.[0-9]{1,})?$|^N$|^Y$");
      let juge3 =  patt3.test(self.data.result)
      if(!juge3){
        self.showModal({msg:'请填写正确检查结果'})
        return
      }
    }
    self.data.num++
    self.setData({
      qcItemCategory:self.data.addList[self.data.num].qcItemCategory,
      qcItem:self.data.addList[self.data.num].qcItem,
      num:self.data.num,
      result:self.data.addList[self.data.num].result,
      thresholdRange:self.data.addList[self.data.num].thresholdRange,
      qcPeriod:self.data.addList[self.data.num].qcPeriod
    })
    
  },
  resulchange(e){
    let self =this
    this.setData({
      result:e.detail.value,
    })
    self.data.addList[self.data.num].result = e.detail.value
console.log(e)
  },
  numless(){
    let self = this
    if(self.data.num==0){
      self.setData({
        syy:true
      })

    }else{
      self.setData({
        xyy:false
      })
    }
    self.data.addList[self.data.num].result = self.data.result
    self.data.num--
    self.setData({
      qcItemCategory:self.data.addList[self.data.num].qcItemCategory,
      qcItem:self.data.addList[self.data.num].qcItem,
      result:self.data.addList[self.data.num].result,
      num:self.data.num,
      thresholdRange:self.data.addList[self.data.num].thresholdRange,
      qcPeriod:self.data.addList[self.data.num].qcPeriod
    })
  },
  toEqDetail(){

  },
  saveData(){
    let self =this
    self.data.addList.forEach((e, index) => {
      self.data.addList[index].projectId = e.id;
    });

    let obj = {
      status: "1",
      type:1,
      insertTime: self.data.date,
      code: self.data.code,
      list: self.data.addList,
    };

    // wx.showModal({
    //   title: '提示',
    //   content: '确定删除？',
    //   success: function (res) {
    //     if (res.confirm) {
    //       console.log('确定')
    //     } else {
    //       console.log('取消')
    //     }
    //   }
    // })

    self
    .axios
    .addOrUpdate("QualityControlInformation", obj, false)
    .then((res) => {
      //self.onReady()
      if(res.code==200){
        wx.showToast({
          title: '新增成功',
          icon: 'succes',
          duration: 1000,
          success: () => {
            self.Router.push({
              path: "",
              query: {},
              openType: 'back',
              duration: 1000,
            })
          }
      })
      }

    })
    .catch((err) => {

    });
  },
  checkResult(){
    let self = this
    if(self.data.addList.length==1){
      let patt1=new RegExp("^(-{0,1}[0-9])+(.[0-9]{1,})?$");
      let juge =""
      juge = patt1.test(self.data.addList[0].result)
      if(!juge){
        self.showModal({msg:'请填写正确检查结果'})
        return
      }
    }
    self.data.addList.forEach((e, index) => {
      self.data.addList[index].projectId = e.id;
    });
    let juge = true
    self.data.addList.forEach(e=>{
     if(e.result==""){
      juge=false
     } 
    })
    if(!juge){
      self.showModal({msg:'请填写结果'})
      return false
    }
    let obj = {
      status: "1",
      type:1,
      insertTime: self.data.date,
      code: self.data.code,
      list: self.data.addList,
    };

          //判断是是否超过阈值
          self.axios.jrsfRequest("/api/QualityControlRecordCheck/checkRecord",obj, 'POST')
          .then(res=> {
            if(res.data.isOut==1){
              console.log(res)
              let content=""
              content=res.data.content.split(";")
              content.pop()
              console.log(content)
              self.setData({
                iosDialog1:true,
                content:content
              })

            }else{
              self.saveData()
            }
          })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  openIOS1: function() {
    this.setData({
        iosDialog1: true
    });
},
  openToast: function() {
    this.setData({
        toast: true
    });
    setTimeout(() => {
        this.setData({
            hideToast: true
        });
        setTimeout(() => {
            this.setData({
                toast: false,
                hideToast: false,
            });
        }, 300);
    }, 3000);
},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})